package com.zch.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.base.dialect.PaginationSupport;
import com.base.support.ExcelColumns;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;

import com.base.util.ExcelExporter;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import org.springframework.beans.factory.annotation.Autowired;
import com.base.action.CoreController;

import com.zch.dao.model.CompanyRegisterNew;
import com.zch.dao.model.CompanyRegisterNewExample;
import com.zch.daoEx.model.CompanyRegisterNewEx;
import com.zch.service.CompanyRegisterNewService;

@RequestMapping("companyRegisterNew")
@Controller
@LoginFilter
public class CompanyRegisterNewController extends CoreController{

	@Autowired
	private CompanyRegisterNewService companyRegisterNewService;

	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows, String name){
		PaginationSupport.byPage(page, rows);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		List<CompanyRegisterNewEx> list = companyRegisterNewService.selectList(map);
		return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(CompanyRegisterNew companyRegisterNew){
		if (StringUtils.isBlank(companyRegisterNew.getId())){
			companyRegisterNewService.insert(companyRegisterNew);
		}else{
			companyRegisterNewService.updateByPrimaryKeySelective(companyRegisterNew);
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String id){
		CompanyRegisterNew companyRegisterNew = companyRegisterNewService.selectByPrimaryKey(id);
		return JSONUtils.serialize(companyRegisterNew);
	}

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			companyRegisterNewService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}

	 @RequestMapping("/exportExcel")
	    public void exportExcel(HttpServletResponse response, String id) throws Exception{
	        Map<String, Object> map = new HashMap<String ,Object>();
	        CompanyRegisterNewExample example = new CompanyRegisterNewExample();
	        List<CompanyRegisterNewEx> list = companyRegisterNewService.selectExcelList();
	        ExcelExporter exporter = new ExcelExporter();
	        
	        ExcelColumns[] headers = new ExcelColumns[]{
	                new ExcelColumns("companyName","企业名称"),
	                new ExcelColumns("cyTypeName","载体类型"),
	                new ExcelColumns("area","使用面积"),
	                new ExcelColumns("pepole","从业人员"),
	                new ExcelColumns("zaitiName","载体名称"),
	                new ExcelColumns("shudi","属地"),
	                new ExcelColumns("beginTime","起始时间"),
                    new ExcelColumns("endTime","结束时间")
	        };
	        exporter.createWork(list, headers);
	        exporter.export(response, null);
	    }
}